<template>
  <div id="barCharts" class="barCharts" />
</template>
<script>
export default {
  name: 'BarCharts',
  props: {
    data: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      echartData: []
    }
  },
  watch: {
    data: {
      handler(newValue) {

        if (newValue && newValue.length) {
          this.echartData = newValue
          this.getEcharts()
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {

  },
  methods: {
    getEcharts() {
      // 接下来的使用就跟之前一样，初始化图表，设置配置项
      const myChart = this.$echarts.init(document.getElementById('barCharts'))
      myChart.setOption({
        backgroundColor: '#fff',
        color: ['#4D94FF'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.echartData.map(item => item.insuranceRange),
            axisTick: {
              alignWithLabel: true,
              show: false
            },
            axisLine: {
              lineStyle: {
                color: '#C9CDD4'
              }
            }

          }
        ],
        yAxis: [
          {
            name: '家',
            type: 'value',
            splitLine: {
              lineStyle: {
                type: 'dashed'
              }
            },
            axisLine: {
              lineStyle: {
                color: '#C9CDD4'
              }
            }
          }
        ],
        series: [
          {
            name: '数量',
            type: 'bar',
            barWidth: '60%',
            label: {
              show: true,
              position: 'top'
            },
            data: this.echartData.map(item => item.companyCount)
          }
        ]
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.barCharts{
  width: 100%;
  height: 320px;
}
</style>
